package BVRClient;

import org.newdawn.slick.Image;

import BVRClient.BVRC_Player.PLAYER_TEAM;

public class BVRC_Projectile extends BVRC_Entity
{
	private Image sprite = null;
	
	private int movementSpeed = 200;
	private int id = -1;
	private int ownerId = -1;
	
	private BVRC_Rect tempRect = new BVRC_Rect(0, 0, 0, 0);
	
	private PLAYER_TEAM team = PLAYER_TEAM.RED;
	
	/**
	 * 
	 * @param movementSpeed
	 * @param player
	 */
	BVRC_Projectile(int movementSpeed, BVRC_Player player)
	{
		this.team = player.getTeam();
		this.ownerId = player.getPlayerID();
		this.setMovementSpeed(movementSpeed);
		
		switch (this.team)
		{
			case BLUE:
				if (BVRC_ImageResources.blueTeamBullet != null)
					this.sprite = BVRC_ImageResources.blueTeamBullet.copy();
			break;
			
			case RED:
				if (BVRC_ImageResources.redTeamBullet != null)
					this.sprite = BVRC_ImageResources.redTeamBullet.copy();
			break;
		}
		
		if (this.sprite != null)
		{
			this.entityRect.width = this.sprite.getWidth();
			this.entityRect.height = this.sprite.getHeight();
			
			this.sprite.setRotation(player.getRotation());
		}
		
		this.setPosition(player.getPosition());
		this.offsetPosition(player.getBulletStartPosition().x, player.getBulletStartPosition().y);
	}
	
	/**
	 * 
	 * @param delta
	 * @return true if the movement was successful, false in all other cases
	 */
	public boolean updateMovement(int index, int delta, BVRC_Map currentMap)
	{
		if (this.movementSpeed == 0)
			return false;
		
		this.tempRect.x = this.entityRect.x;
		this.tempRect.y = this.entityRect.y;
		
		float velocity = (float)this.movementSpeed * (float)(delta / 1000.0f);
		
		float rotation = this.sprite.getRotation();
		
		this.entityRect.x += velocity * java.lang.Math.sin(java.lang.Math.toRadians(rotation));
		this.entityRect.y -= velocity * java.lang.Math.cos(java.lang.Math.toRadians(rotation));
		
		if (currentMap.rectMapCollision(this.entityRect))
		{
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @param player
	 * @return true if this projectile collides with the player, false in all other cases
	 */
	public boolean playerCollision(BVRC_Player player)
	{
		if (player == null)
			return false;
		
		return player.getEntityRect().rectCollision(entityRect);
		
		//return false;
	}
	
	/**
	 * 
	 * @param camera
	 */
	public void draw(BVRC_Camera camera)
	{
		if (this.sprite == null)
			return;
		
		this.sprite.draw(this.entityRect.x - camera.getVisibleRect().x, this.entityRect.y - camera.getVisibleRect().y);
	}
	
	/**
	 * 
	 * @return the projectile's rotation angle
	 */
	public float getRotation()
	{
		if (sprite == null)
			return 0;
		
		return sprite.getRotation();
	}
	
	/**
	 * 
	 * @param rotation
	 */
	public void setRotation(float rotation)
	{
		if (sprite == null)
			return;
		
		sprite.setRotation(rotation);
	}
	
	/**
	 * 
	 * @return the projectile's team
	 */
	public PLAYER_TEAM getTeam()
	{
		return team;
	}
	
	/**
	 * 
	 * @param team
	 */
	public void setTeam(PLAYER_TEAM team)
	{
		this.team = team;
	}
	
	/**
	 * 
	 * @param movementSpeed
	 */
	public void setMovementSpeed(int movementSpeed)
	{
		this.movementSpeed = movementSpeed;
	}
	
	/**
	 * 
	 * @return the projectile's movement speed
	 */
	public int getMovementSpeed()
	{
		return movementSpeed;
	}
	
	/**
	 * 
	 * @param id
	 */
	public void setId(int id)
	{
		this.id = id;
	}
	
	/**
	 * 
	 * @return the projectile's id
	 */
	public int getId()
	{
		return id;
	}
	
	/**
	 * 
	 * @param ownerId
	 */
	public void setOwnerId(int ownerId)
	{
		this.ownerId = ownerId;
	}
	
	/**
	 * 
	 * @return the projectile's owner's id (who fired the projectile)
	 */
	public int getOwnerId()
	{
		return ownerId;
	}
}
